//
//  STCustomTF.m
//  赛拓
//
//  Created by 王磊 on 7/31/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STCustomTF.h"
@interface STCustomTF()<UITextFieldDelegate>

@property (nonatomic ,strong) UIView *bottomLine;

@property (nonatomic ,strong) UIButton *verifyItem;

@property (nonatomic ,strong) UIImageView *iconImageView;

@property (nonatomic ,strong) UIView *verticalLine;

@property (nonatomic ,strong) UITextField *tf;

@end
@implementation STCustomTF

#define k_icon_width 24 * ST_RATE

#define k_icon_height 30 * ST_RATE
- (void)setIsVerifyShow:(BOOL)isVerifyShow {
    _isVerifyShow = isVerifyShow;
    
    self.verifyItem.hidden = isVerifyShow;
    
    self.verticalLine.hidden = isVerifyShow;
    
}
- (void)setIconImageName:(NSString *)iconImageName {
    _iconImageName = iconImageName;
    
    UIImage *image = [UIImage imageNamed:iconImageName];
    
    self.iconImageView.image = image;
}
- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    
    self.tf.attributedPlaceholder = [[NSAttributedString alloc]initWithString:placeHolder attributes:@{NSFontAttributeName: ST_15_FONT ,NSForegroundColorAttributeName: ST_999999_COLOR}];
}
- (void)setSecureTextEntry:(BOOL)secureTextEntry {
    _secureTextEntry = secureTextEntry;
    
    self.tf.secureTextEntry = secureTextEntry;
    
}
- (void)commitInit {
    [super commitInit];
    
    self.secureTextEntry = false;
    
    UIView *bottomLine = [[UIView alloc]init];
    
    self.bottomLine = bottomLine;
    
    bottomLine.backgroundColor = ST_EDGE_COLOR;
    
    [self addSubview:bottomLine];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    
    self.iconImageView = imageView;
    
    [self addSubview:imageView];
    
    UITextField *tf = [[UITextField alloc]init];
    
    self.tf = tf;
    
    tf.delegate = self;
    
    [self addSubview:tf];
    
    tf.font = ST_15_FONT;
    
    tf.textColor = ST_666666_COLOR;
    
    UIView *verticalLine = [[UIView alloc]init];
    
    [tf addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventEditingChanged];
    
    verticalLine.backgroundColor = ST_EDGE_COLOR;
    
    self.verticalLine = verticalLine;
    
    [self addSubview:verticalLine];
    
    UIButton *verify = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.verifyItem = verify;
    
    verify.titleLabel.font = ST_15_FONT;
    
    [verify setTitle:@"点击获取" forState:UIControlStateNormal];
    
    [verify setTitle:@"点击获取" forState:UIControlStateHighlighted];
    
    [verify setTitleColor:ST_167EC0_COLOR forState:UIControlStateNormal];
    
    [verify setTitleColor:ST_167EC0_COLOR forState:UIControlStateDisabled];
    
    [self addSubview:verify];
    
    [verify addTarget:self action:@selector(verifyItemTriggered) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundColor = ST_CLEAR_COLOR;
    
    self.isVerifyShow = true;
    
    self.maxLength = 11;
    
    self.backgroundColor = ST_F2F2F2_COLOR;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = self.k_height;
    
    CGFloat margin = height / 2;
    
    self.layer.cornerRadius = margin;
    
    [self.iconImageView  makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(margin);
        make.centerY.equalTo(0);
        make.width.equalTo(k_icon_width);
        make.height.equalTo(k_icon_height);
    }];
    @WeakObj(self)
    [self.tf makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(height);
        make.centerY.equalTo(0);
        make.height.equalTo(height);
        make.right.equalTo(0);
    }];
    
    CGSize verify_size = [NSString suitToSize:@"点击获取" withSize:CGSizeMake(999, 999) andFont:ST_15_FONT andLineSpacing:3];
    
    [self.verifyItem makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-margin);
        make.centerY.equalTo(0);
        make.width.equalTo(verify_size.width);
        make.height.equalTo(height);
    }];
    
    [self.verticalLine makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(0);
        make.height.equalTo(height - margin);
        make.width.equalTo(0.5);
        make.right.equalTo(selfWeak.verifyItem.mas_left).offset(-margin);
    }];
}
- (void)verifyItemTriggered {
    
    [self.mDelegate onItemClick:STItemClickTypeVerify withData:nil forIndexPath:nil];
}
- (void)updateVerifyState:(NSString *)title isEnabled:(BOOL) isEnabled {
    
    self.verifyItem.enabled = isEnabled;
    
    UIControlState state = isEnabled ? UIControlStateNormal : UIControlStateDisabled;
    
    [self.verifyItem setTitle:title forState:state];
}
- (void)valueChanged:(UITextField *)tf {
    
    self.text = tf.text;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self.mDelegate onItemClick:STItemClickTypeBeginEditing withData:nil forIndexPath:nil];
    return true;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (range.length == 1 && string.length == 0)
    {
        return YES;
    }
    else  {
        
        return textField.text.length < self.maxLength;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self.mDelegate onItemClick:STItemClickTypeReturnKey withData:self.tid forIndexPath:nil];
    
    return true;
}


- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType {
    _returnKeyType = returnKeyType;
    
    self.tf.returnKeyType = returnKeyType;
}
- (void)clear {
    
    self.tf.text = @"";
    
    self.text = @"";
}

- (void)becomeFirstResponder {
    
    [self.tf becomeFirstResponder];
    
}
-(void)resignFirstResponder {
    
    [self.tf resignFirstResponder];
}

@end
